package com.kingint.web.sys.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kingint.common.unitedresult.K;
import com.kingint.mbg.dto.UmsAdminLoginParam;
import com.kingint.mbg.sys.entity.UmsMember;
import com.kingint.mbg.sys.entity.UmsRole;
import com.kingint.web.sys.service.UmsPermissionService;
import com.kingint.web.sys.service.UmsRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.kingint.web.sys.service.UmsAdminService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @描述：后台用户表控制类
 * @作者: wxh
 * @日期: 2020-09-18
 */
@RestController
@RequestMapping("/admin")
@Api(value = "UmsAdminController", description = "后台用户表")
@CrossOrigin
public class UmsAdminController extends BaseController{

    @Autowired
    private UmsAdminService adminService;
    @Resource
    private UmsPermissionService permissionService;
    @Resource
    private UmsRoleService roleService;

    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;


    @ApiOperation(value = "登录以后返回token")
    @PostMapping(value = "/login")
    public Object login(@Validated @RequestBody UmsAdminLoginParam umsAdminLoginParam) {
        String token = adminService.login(umsAdminLoginParam);
        if (token == null) {
            return K.error().data("用户名或密码错误");
        }
        Map<String, Object> tokenMap = new HashMap<>();
        tokenMap.put("token", token);
        tokenMap.put("tokenHead", tokenHead);
//        tokenMap.put("menus",permissionService.getPermissionsByUserId(getCurrentUser().getId()));
        return K.ok().data(tokenMap);
    }


    @ApiOperation(value = "获取用户信息")
    @GetMapping(value = "/getUserInfo")
    public Object getUserInfo() {
        UmsMember currentMember = adminService.getCurrentMember();
        return K.ok().data(currentMember);
    }

    @ApiOperation(value = "刷新token")
    @GetMapping(value = "/token/refresh")
    public Object refreshToken(HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        String refreshToken = adminService.refreshToken(token);
        if (refreshToken == null) {
            return K.error();
        }
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", token);
        tokenMap.put("tokenHead", tokenHead);
        return K.ok().data(tokenMap);
    }

    @ApiOperation(value = "获取当前登录用户信息")
    @GetMapping(value = "/info")
    public Object getAdminInfo(Principal principal) {
        String username = principal.getName();
        UmsMember umsAdmin = adminService.getAdminByName(username);
        Map<String, Object> data = new HashMap<>();
        data.put("username", umsAdmin.getUsername());
        data.put("roles", new String[]{"TEST"});
        data.put("icon", umsAdmin.getIcon());
        return K.ok().data(data);
    }

    @ApiOperation(value = "登出功能")
    @PostMapping(value = "/logout")
    public Object logout() {
        return K.ok().data(null);
    }



    @ApiOperation("获取指定用户信息")
    @GetMapping(value = "/{id}")
    public Object getItem(@PathVariable Long id) {
        UmsMember item = adminService.getById(id);
        return K.ok().data(item);
    }

    @ApiOperation("获取指定用户的角色")
    @GetMapping(value = "/userRoleCheck")
    public Object userRoleCheck(@RequestParam("adminId") Long adminId) {
        List<UmsRole> roleList = adminService.getRoleList(adminId);
        QueryWrapper<com.kingint.mbg.sys.entity.UmsRole> wrapper = new QueryWrapper<>();
        List<UmsRole> allroleList = roleService.list(wrapper);
        for (UmsRole a : allroleList){
            for (UmsRole u : roleList){
                if(a.getId().equals(u.getId())){
                    a.setChecked(true);
                }
            }
        }
        return K.ok().data(allroleList);
    }
}